﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace BenevolentSunData
{
    public class PrismCell : Cell
    {

        public override string Name
        {
            get
            {
                if (isActive)
                    return "Prism";
                else
                    return "Empty Prism Stand";
            }
        }

        public override string Note
        {
            get
            {
                if (isActive)
                {
                    return "Pickup the prism to move it elsewhere.";
                }
                else
                {
                    return "Place a prism onto this stand to combine and split beams that hit it.";
                }
            }
        }


        #region State
        Boolean isActive = true;
        public Boolean IsActive
        {
            get { return isActive; }
            set
            {
                isActive = value;
            }
        }
        #endregion


        #region Graphics
        Model prismModel;

        Matrix[] boneTransforms;

        Material prismMaterial, standMaterial;

        Vector3 originalPrismEmissive;
        #endregion


        #region Drawing
        public override void Draw(Vector3 position)
        {
            foreach (ModelMesh mesh in prismModel.Meshes)
            {
                Material material;
                if (mesh.ParentBone.Parent.Name == "Prism")
                {
                    if (!isActive)
                        continue;
                    material = prismMaterial;
                }
                else
                {
                    material = standMaterial;
                }

                Matrix world =
                    boneTransforms[mesh.ParentBone.Index] *
                    Matrix.CreateFromYawPitchRoll(Rotation, 0f, 0f) *
                    Matrix.CreateTranslation(position);
                material.DrawMesh(mesh, world, Map.PointLights, Map.BeamLights);
            }
        }

        #endregion


        #region Initialization
        public PrismCell()
        {
            //DiffuseColor = new Vector3(0.6f, 0.6f, 0.1f);
        }

        public override void LoadContent(ContentManager Content, Map map)
        {
            prismModel = Content.Load<Model>("Models/Prism");
            boneTransforms = new Matrix[prismModel.Bones.Count];
            prismModel.CopyAbsoluteBoneTransformsTo(boneTransforms);

            prismMaterial = map.Skin.GetMaterial("Prism");
            standMaterial = map.Skin.GetMaterial("Prism Stand");
            originalPrismEmissive = prismMaterial.EmissiveColor.ToVector3();

            IsActive = isActive;

            base.LoadContent(Content, map);
        }
        #endregion


        #region Beams
        public override void Clear()
        {
            prismMaterial.EmissiveColor = new Color(originalPrismEmissive);
            base.Clear();
        }

        public override List<Direction> AddInput(Beam beam, Direction direction)
        {
            MergeBeam(beam, direction);

            List<Direction> output = new List<Direction>();

            if (IsActive)
            {
                prismMaterial.EmissiveColor = new Color((originalPrismEmissive + (GetBeam(direction).Color.ToVector3() / 1.5f)) / 2.0f);

                if (MergeBeam(beam, Direction.North))
                    output.Add(Direction.North);
                if (MergeBeam(beam, Direction.South))
                    output.Add(Direction.South);
                if (MergeBeam(beam, Direction.East))
                    output.Add(Direction.East);
                if (MergeBeam(beam, Direction.West))
                    output.Add(Direction.West);
            }
            else
            {
                switch (direction)
                {
                    case Direction.North:
                        MergeBeam(beam, Direction.North);
                        if (MergeBeam(beam, Direction.South))
                            output.Add(Direction.South);
                        break;
                    case Direction.South:
                        MergeBeam(beam, Direction.South);
                        if (MergeBeam(beam, Direction.North))
                            output.Add(Direction.North);
                        break;
                    case Direction.East:
                        MergeBeam(beam, Direction.East);
                        if (MergeBeam(beam, Direction.West))
                            output.Add(Direction.West);
                        break;
                    case Direction.West:
                        MergeBeam(beam, Direction.West);
                        if (MergeBeam(beam, Direction.East))
                            output.Add(Direction.East);
                        break;
                }
            }


            return output;
        }
        #endregion


        #region Interactions
        public override bool Interact()
        {
            IsActive = !IsActive;
            
            return true;
        }
        #endregion


        #region ICloneable
        public override object Clone()
        {
            PrismCell cell = new PrismCell();
            cell.prismModel = prismModel;
            cell.Coords = new Point(Coords.X, Coords.Y);
            cell.boneTransforms = boneTransforms;
            cell.standMaterial = standMaterial.Clone();
            cell.prismMaterial = prismMaterial.Clone();
            cell.originalPrismEmissive = originalPrismEmissive;
            cell.floorBoneTransforms = floorBoneTransforms;
            cell.floorModel = floorModel;
            cell.floorMaterial = floorMaterial.Clone();

            cell.IsActive = isActive;

            return cell;
        }
        #endregion

    }
}
